 // 地图
 Ext.define('qjtravel.view.Map', {
     extend: 'Ext.Container',
     xtype: 'map',
     id:'map',

     config: {
         id: "baidumap",

         html: "正在加载地图...",
         listeners: {
             initialize: function() {
                 // var script = document.createElement("script");
                 // script.src = "http://api.map.baidu.com/api?v=1.4&callback=qjtravel.view.Map.draw";
                 // document.body.appendChild(script);

             },
             activate: function() {
                 //console.log('activate map');
                 this.initBaidumap();
                 this.search();
             }
         }
     },
     isMap: false,
     driving: function() {
         console.log('arguments1');
         var bmap = this.baidumap;
         console.log('arguments1' + bmap);
         var area = this.area;
         console.log('arguments1' + area);
         var onSuccess = function(position) {
             console.log(position);
             var gpsPoint = new BMap.Point(
                 position.coords.longitude,
                 position.coords.latitude);

             BMap.Convertor.translate(gpsPoint, 0, function(point) {
                 bmap.clearOverlays();
                 var driving = new BMap.DrivingRoute(bmap, {
                     renderOptions: {
                         map: bmap,
                         autoViewport: true
                     }
                 });
                 driving.search(point, area);

             });
             Ext.Viewport.unmask();
         };

         // onError Callback receives a PositionError object
         //

         function onError(error) {
             //cordova.exec(function (message) { }, function (error) {}, "Messages", "showMsg", [error.message]);
         }

         navigator.geolocation.getCurrentPosition(onSuccess, onError);
     },
     baidumap: null,
     zoomCtrl: null,
     driving: function(lng, lat) {
         Ext.Viewport.mask({
             xtype: 'loadmask',
             message: '加载中...'
         });

         var bmap = this.baidumap;
         var _this = this;

         var onSuccess = function(position) {
             var gpsPoint = new BMap.Point(
                 position.coords.longitude,
                 position.coords.latitude);

             BMap.Convertor.translate(gpsPoint, 0, function(point) {
                 bmap.clearOverlays();
                 // var marker = new BMap.Marker(point);
                 //bmap.addOverlay(marker);
                 //var label = new BMap.Label("我是百度标注哦",{offset:new BMap.Size(20,-10)});
                 //marker.setLabel(label); //添加百度label
                 // bmap.setCenter(point);
                 //alert(point.lng + "," + point.lat);
                 var driving = new BMap.DrivingRoute(bmap, {
                     renderOptions: {
                         map: bmap,
                         autoViewport: true
                     }
                 });
                 driving.search(point, new BMap.Point(lng, lat));
                 _this.addControl();
             });
             Ext.Viewport.unmask();
         };

         // onError Callback receives a PositionError object
         //
         var onError = function(error) {
             cordova.exec(function(message) {}, function(error) {}, "Messages", "showMsg", [error.message]);
             Ext.Viewport.unmask();
         }

         navigator.geolocation.getCurrentPosition(onSuccess, onError);
     },
     addControl: function() {
         // 定义一个控件类,即function
         var bmap = this.baidumap;
         var _this = this;

         function ZoomControl() {
             // 默认停靠位置和偏移量
             this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
             this.defaultOffset = new BMap.Size(10, 10);
         }
         // 通过JavaScript的prototype属性继承于BMap.Control
         ZoomControl.prototype = new BMap.Control();

         // 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
         // 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
         ZoomControl.prototype.initialize = function(map) {
             // 创建一个DOM元素
             var div = document.createElement("div");
             // 添加文字说明
             div.appendChild(document.createTextNode("退出导航"));
             // 设置样式
             div.style.cursor = "pointer";
             div.style.border = "3px solid #060606";
             div.style.color = '#fff';
             div.style.borderRadius = '5px';
             div.style.padding = "6px";
             // div.class="x-button-normal x-button x-layout-box-item x-stretched"
             div.style.backgroundColor = "#383838";
             // 绑定事件,点击一次放大两级
             div.onclick = function(e) {
                   Ext.Viewport.mask({ xtype: 'loadmask',message: '加载中...' });
                 bmap.removeControl(_this.zoomCtrl);
                 _this.search();
             }
             // 添加DOM元素到地图中
             bmap.getContainer().appendChild(div);
             // 将DOM元素返回
             return div;
         }
         // 创建控件
         this.zoomCtrl = new ZoomControl();
         // 添加到地图当中
         bmap.addControl(this.zoomCtrl);
     },
     initBaidumap: function() {
         if (!this.isMap && BMap != 'undefined') {
             this.isMap = true;
             var map = new BMap.Map('baidumap');

             map.addControl(new BMap.NavigationControl());
             map.addControl(new BMap.ScaleControl());
             var point = new BMap.Point(108.953457,34.265688);
             console.log(point)
             map.centerAndZoom(point, 16);
             map.addOverlay(new BMap.Marker(point));

             map.addControl(new BMap.OverviewMapControl());
             map.addControl(new BMap.MapTypeControl());
             map.setCurrentCity("西安");
             map.addEventListener("tilesloaded", function(){
                 var alllink= Ext.query('#baidumap .BMap_cpyCtrl');
                 alllink.length > 0 ?alllink[0].style.display='none' : "";
                 for(var ims=0;ims<Ext.query('#baidumap a').length;ims++){
                     var links=Ext.query('#baidumap a')[ims].href;
                     if(links.indexOf('baidu')!=-1){
                         Ext.query('#baidumap a')[ims].href = "javascript:void(0);";
                     }
                 }
             });
             var point = new BMap.Point(108.953451, 34.265773);
             map.centerAndZoom(point, 12);
             this.baidumap = map;
         }
     },
     search: function() {
         var map = this.baidumap;
         var local = new BMap.LocalSearch("西安市", {
             renderOptions: {
                 map: map
                 } 
             });
         // if (Ext.os.is.Android) {
         local.setInfoHtmlSetCallback(function(poi) {
             var aLink = Ext.query('.BMap_pop a');
             var url = aLink[0].href;
             aLink[0].href = "javascript:Ext.getCmp('baidumap').driving(" + poi.point.lng + ", " + poi.point.lat + ");void(0)";
             aLink[0].innerHTML = '导航';

         });

         // }
         // local.enableFirstResultSelection();
         // local.search("景点");
         map.clearOverlays(); 
         local.searchNearby("景点", '曲江新区');
         Ext.Viewport.unmask();

         }
     });